package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by 甘静斌 on 2020-08-16 23:40
 */
@Service
public class StrategyCommentRedisServiceImpl implements IStrategyVORedisService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;



    @Override
    public void viewIncreates(String sid, int num) {

        //String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);

        StrategyStatisVO vo = this.getStatisVo(sid);
        //更新
        vo.setViewnum(vo.getViewnum() + num);
        this.setStatisVo(vo);
        //template.opsForValue().set(key,JSON.toJSONString(vo));



       /* //拼接key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断key是否存在
        StrategyStatisVO vo = null;
        if (!template.hasKey(key)){
            //如果key不存在,初始化对象vo
            vo = new StrategyStatisVO();
            //根据sid获取攻略对象
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo );
            //strategyId没有,需要属性名字必须一致的时候才可以复制
            vo.setStrategyId(strategy.getId());
        }else {
            //如果key存在
            String str = template.opsForValue().get(key);
            //因为str是字符串
            vo = JSON.parseObject(str,StrategyStatisVO.class);
        }

        //更新
        vo.setViewnum(vo.getViewnum() + num);
        template.opsForValue().set(key,JSON.toJSONString(vo));
*/
    }

    @Override
    public StrategyStatisVO getStatisVo(String sid) {

        //根据攻略sid 拼接key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);

        StrategyStatisVO vo = null;
        //判断key是否存在,当key不存在时
        if (!template.hasKey(key)){
            //初始化vo对象
            vo = new StrategyStatisVO();
            //根据攻略id获取原始的攻略对象
            Strategy strategy = strategyService.get(sid);
            //复制strategy对象,给到vo对象
            BeanUtils.copyProperties(strategy,vo );
            vo.setStrategyId(strategy.getId());
        }else {
            //当key存在时,
            String str = template.opsForValue().get(key);
            vo = JSON.parseObject(str,StrategyStatisVO.class);
        }

        //返回vo对象
        return vo;
    }

    @Override
    public void setStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public void replynumIncreates(String sid, int num) {

        StrategyStatisVO vo = this.getStatisVo(sid);
        vo.setReplynum(vo.getReplynum() + num);
        this.setStatisVo(vo);


    }

    @Override
    public boolean favorIncreaces(String sid, UserInfo userInfo) {

        //根据用户id拼接出key(获取记号,攻略id集合)
        String key = RedisKeys.STRATEGY_FAVOR_VO.join(userInfo.getId());
        //定义一个空的数组,记号不存在,初始化
        List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)){
            //如果key(记号)存在直接获取sidList
            String strkey = template.opsForValue().get(key);
            //将字符串转换成list集合,参数2:集合泛型
            sidList = JSON.parseArray(strkey,String.class );
        }
        //通过记号判断当前操作是收藏还是取消收藏
        StrategyStatisVO vo = this.getStatisVo(sid);
        if (sidList.contains(sid)){
            //如果存在,取消收藏
            //获取vo对象
            vo.setFavornum(vo.getFavornum() -1);
            sidList.remove(sid);
        }else {
            //如果不存在,点击收藏
            vo.setFavornum(vo.getFavornum() + 1);
            sidList.add(sid);
        }
        //更新sidList集合,更新vo
        template.opsForValue().set(key,JSON.toJSONString(sidList));
        this.setStatisVo(vo);
        //返回sid有就为true
        return sidList.contains(sid);
    }





   /* String key = RedisKeys.STRATEGY_FAVOR_VO.join(userInfo.getId());
    List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)){
        //如果key存在,直接获取sidList
        String strkey = template.opsForValue().get(key);
        sidList = JSON.parseArray(strkey,String.class);
    }

    StrategyStatisVO vo = null;
        if (sidList.contains(sid)){
        vo = this.getStatisVo(sid);
        vo.setFavornum(vo.getFavornum() - 1);
        sidList.remove(sid);
    }else {
        vo = this.getStatisVo(sid);
        vo.setFavornum(vo.getFavornum() + 1);
        sidList.add(sid);
    }

        template.opsForValue().set(key,JSON.toJSONString(sidList));

        this.setStatisVo(vo);

        return sidList.contains(sid);
}*/
    @Override
    public boolean strategyThumbup(String sid, String uid) {

        //根据sid,uid拼接记号key
        String key = RedisKeys.STRATEGY_THUMBUP_VO.join(sid, uid);
        StrategyStatisVO vo = null;
        //判断key是否存在
        if (!template.hasKey(key)){
            //不存在,获取vo
            vo = this.getStatisVo(sid);
            //点赞数 +1
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            //缓存到redis上
            this.setStatisVo(vo);
            //设置过期时间
            Date now = new Date();
            Date endDate = DateUtil.getEndDate(new Date());

            template.opsForValue().set(key,"1" , DateUtil.getDateBetween(now,endDate ),TimeUnit.SECONDS );
            //更新
            this.setStatisVo(vo);
            return true;
        }


        return false;
    }

    @Override
    public boolean isStrategyVoExist(String id) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVos() {

        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");
        //所有vo对象的key值
        Set<String> vokeys = template.keys(pattern);

        List<StrategyStatisVO> list = new ArrayList<>();
        if (vokeys != null && vokeys.size() > 0){
            for (String vokey : vokeys) {
                String strVo = template.opsForValue().get(vokey);
                list.add(JSON.parseObject(strVo,StrategyStatisVO.class ));
            }
        }
        return list;
    }

    @Override
    public List<String> queryByFavor(String sid) {

        //根据用户id拼接出key(获取记号,攻略id集合)
        String key = RedisKeys.STRATEGY_FAVOR_VO.join(sid);
        //定义一个空的数组,记号不存在,初始化
        List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)){
            //如果key(记号)存在直接获取sidList
            String strkey = template.opsForValue().get(key);
            //将字符串转换成list集合,参数2:集合泛型
            sidList = JSON.parseArray(strkey,String.class );
        }

        return sidList;
    }


}
